<?php
/**
 * Object Relational Persistence Mapping Class for table: lab_ticket
 * @package modules.dept_laboratory.ordo
 * @author viethd
 * @version $Id$
 * @copyright 2009 LHV Company
 */
class LabTicket extends ORDataObject {

	/**#@+
	 * Fields of table: lab_ticket mapped to class members
	 */
	var $lab_ticket_id		= '';
	var $lab_assignment_ticket_id		= '';
	var $technician1_id		= '';
	var $technician2_id		= '';
	var $name1		= '';
	var $name2		= '';
	var $is_done		= '';
	var $lab_group_id		= '';
	var $machine_name		= '';
	var $conclusion		= '';
	var $modified     = '';
	/**#@-*/


	/**
	 * DB Table
	 */
	var $_table = 'lab_ticket';

	/**
	 * Primary Key
	 */
	var $_key = 'lab_ticket_id';

	/**
	 * Internal Name
	 */
	var $_internalName = 'LabTicket';

	/**
	 * Handle instantiation
	 */
	function __construct() {
		parent::ORDataObject();
	}

	public function setData($labTicketInfo) {
		if (is_array($labTicketInfo)) {			
			$this->populateArray($labTicketInfo);
		}
	}
	/**
	 * List all lab ticket of given labAssignmentTicketId.
	 * @param $labAssignmentTicketId
	 * @return array(lab_ticket_id);
	 */
	public function listLabTickets($labAssignmentTicketId, $short = false) {
		$field    =   L10nHelper::changeField(L10nHelper::getLocale(),"name");
		$ds =& new Datasource_sql();
		if ($short == false) {
			$sql = array(
            'cols'  => "lab_ticket_id, t.lab_group_id, lg.$field name, t.is_done ,technician1_id, technician2_id, machine_name, modified ",
            'from'  => "$this->_table t INNER JOIN lab_group lg ON(t.lab_group_id=lg.lab_group_id)",              
            'where' =>   "lab_assignment_ticket_id ='$labAssignmentTicketId'"            
			);
		} else {
			$sql = array(
            'cols'  => "lab_ticket_id, lab_group_id, is_done, technician1_id, technician2_id, machine_name, modified ",
            'from'  => "$this->_table",              
            'where' =>   "lab_assignment_ticket_id ='$labAssignmentTicketId'"            
            );			 
		}
		$ds->setup($this->_db, $sql);
		return $ds->toArray();
	}
	
    public function getLabTicket($short = false) {
    	$field    =   L10nHelper::changeField(L10nHelper::getLocale(),"name");
        $ds =& new Datasource_sql();
        if ($short == false) {
            $sql = array(
            'cols'  => "lab_ticket_id, t.lab_group_id, lg.$field name,technician1_id, name1, technician2_id, name2, t.is_done, machine_name, modified ",
            'from'  => "$this->_table t INNER JOIN lab_group lg ON(t.lab_group_id=lg.lab_group_id)",              
            'where' =>   "lab_ticket_id ='". $this->lab_ticket_id . "'"            
            );
        } else {
            $sql = array(
            'cols'  => "lab_ticket_id, lab_group_id, is_done, technician1_id, name1, technician2_id, name2, machine_name, modified ",
            'from'  => "$this->_table",              
            'where' =>   "lab_ticket_id ='". $this->lab_ticket_id . "'"             
            );           
        }
        $ds->setup($this->_db, $sql);
        $result = $ds->toArray();
        if (empty($result)) {
        	return null;
        }
        return $result[0];
    }


	/**
	 * Delete All labticket of a  given $labAssignmentTicketId.
	 * @param $labAssignmentTicketId
	 * @return $labAssignmentTicketId
	 */
	public function deleleLabTickets($labAssignmentTicketId) {
		$sql = "DELETE FROM $this->_table WHERE lab_assignment_ticket_id='$labAssignmentTicketId'";
		$this->_execute($sql);
		return $labAssignmentTicketId;
	}
	
    public function setDone($done) {      
        $sql = "UPDATE $this->_table SET is_done='$done' WHERE $this->_key='$this->lab_ticket_id'";
        $this->_execute($sql);
        $this->is_done = $done;
    }


}
?>
